//领料单ID
var materId = getQueryString("materId");
//系统状态
var flag = getQueryString("flag");
//工单编号
var gdbh = getQueryString("gdbh");
$(function () {

    var url = '../materialrecord/list?material=' + materId;
    $("#jqGrid").Grid({
        url: url,
        colModel: [
            {label: '备件编码', name: 'spareCode', index: 'SPARE_CODE', width: 80},
            {label: '备件名称', name: 'spareName', index: 'SPARE_NAME', width: 80},
            {label: '规格型号', name: 'spec', index: 'SPEC', width: 80},
            {label: '仓库代码', name: 'storeCode', index: 'storeCode', width: 80},
            {label: '仓库名称', name: 'storeName', index: 'storeName', width: 80},
            {label: '备件位置', name: 'postion', index: 'postion', width: 80},
            {label: '申请数', name: 'num', index: 'NUM', width: 80},
            {label: '实发数', name: 'actualNum', index: 'ACTUAL_NUM', width: 80},
            {label: '已使用数', name: 'replaceNum', index: 'REPLACE_NUM', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: 'store', name: 'store', index: 'store', hidden: true},
            {name: 'delFlag', hidden: true}
        ],
        gridComplete: function () {
            var ids = $('#jqGrid').getDataIDs();
            for (var i = 0; i < ids.length; i++) {
                var rowData = $('#jqGrid').getRowData(ids[i]);
                if (rowData.delFlag == 0) {
                    $('#' + ids[i]).find("td").addClass("urgeCss");
                }
            }
        }
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        materialRecord: {
            store: '',
            dept: '',
            material: materId,
            num: 0,
            spareCode: '',
            id: '',
            postionId: '',
            postion: ''
        },
        ruleValidate: {
            spareCode: [
                {required: true, message: '物料必选！', trigger: 'blur'}
            ],
            num: [
                {required: true, type: 'number', message: '领取数量必须为数字且不能为空', trigger: 'blur'}
            ]
        },
        q: {
            spareCode: ''
        },
        spareName: '',
        storeName: '',
        spec: '',
        postion: '',
        storeNum: 0,
        locNum: 0
    },
    methods: {
        query: function () {
            vm.reload();
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            // 领用数不得小于1
            let num = Number(vm.materialRecord.num);
            if (num < 1) {
                iview.Message.error("领用数量不得小于1");
                return;
            }
            // 获取冻结数和本次希望领取的备件数
            var nowNum = Number(vm.locNum) + num;
            var nu = nowNum - Number(vm.storeNum);
            if (flag == 1) {
                iview.Message.error("待审核、待出库或出库完毕后禁止编辑！");
            } else {
                if (nu > 0) {
                    iview.Message.error('领取数量不能大于可用库存数量!');
                    return;
                } else {
                    let url = vm.materialRecord.id == (null || '') ? "../materialrecord/save" : "../materialrecord/update";
                    Ajax.request({
                        url: url,
                        params: JSON.stringify(vm.materialRecord),
                        type: "POST",
                        contentType: "application/json",
                        successCallback: function (r) {
                            alert('操作成功', function (index) {
                                vm.materialRecord = {
                                    store: '',
                                    dept: '',
                                    material: materId,
                                    num: 0,
                                    spareCode: '',
                                    id: '',
                                    postionId: '',
                                    postion: ''
                                }
                                vm.spareName = '';
                                vm.storeName= '';
                                vm.spec= '';
                                vm.postion= '';
                                vm.storeNum = 0;
                                vm.locNum = 0;
                                vm.reload();
                            });
                        }
                    });
                }
            }
        },
        del: function (event) {

            if (flag == 1) {
                iview.Message.error("审核通过或者领料单结束之后禁止编辑！");
            } else {

                let ids = getSelectedRows("#jqGrid");
                if (ids == null) {
                    return;
                }
                confirm('确定要删除选中的记录？', function () {
                    Ajax.request({
                        url: "../materialrecord/delete",
                        params: JSON.stringify(ids),
                        type: "POST",
                        contentType: "application/json",
                        successCallback: function () {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        }
                    });
                });
            }
        },

        reload: function (event) {
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'spareCode': vm.q.spareCode},
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            vm.q = {
                spareCode: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        getSpareInfo: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择备件',
                content: '../spare/sparestock_choose.html?gdbh=' + gdbh,
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getSpareStroe();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        let storeNum = parseInt(res.storeNum);
                        if (storeNum == 0 || (storeNum - parseInt(res.locNum) <= 0)) {
                            alert("可用库存不足，请更换!");
                            return;
                        }
                        layer.close(index);
                        vm.materialRecord.spareCode = res.spareCode;
                        vm.spareName = res.spareName;
                        vm.locNum = res.locNum;
                        vm.storeNum = res.storeNum;
                        vm.storeName= res.storeName;
                        vm.spec= res.spec;
                        vm.postion= res.postion;
                        vm.materialRecord.postion = res.postion;
                        vm.materialRecord.store = res.storeId;
                        vm.materialRecord.postionId = res.id;
                        vm.materialRecord.spareName = res.spareName;
                        // 默认为1
                        vm.materialRecord.num = 1;
                    }
                }
            })
        }
    }
});
